package com.example.Lezedata.ui.activity.common;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.Lezedata.R;
import com.example.Lezedata.dataManager.LoginManager;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.login.LoginVc;
import com.google.gson.Gson;

import java.util.HashMap;

public class WebVc extends GBBaseVc {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web_vc;
    }

    WebView webView;

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        needAddViewToSuperView = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_vc);
        baseContentView = findViewById(R.id.contentView);
        findView();
        load();
    }

    void findView(){
        webView = findViewById(R.id.webView);
        WebSettings wSet = webView.getSettings();
        wSet.setJavaScriptEnabled(true);
        wSet.setJavaScriptCanOpenWindowsAutomatically(true);
        webView.setWebViewClient(new WebViewClient(){
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;//ture为在webview中打开
            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                hudShow();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                hudDismiss();
            }

        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Gson gson = new Gson();
                HashMap<String,String> map = null;
                try {
                    map = gson.fromJson(message,HashMap.class);
                }catch (Exception e){
                    Log.d("TAG", "onJsAlert: Json 解析错误 == " + message);
                }
                if (map != null){
                    return handlerJsMessage(map);
                }
                return super.onJsAlert(view, url, message, result);
            }
        });
    }

    boolean handlerJsMessage(HashMap<String,String> map){
        String key = map.get("key");
        if (key.contentEquals("close")){
            finish();
        }
        return true;
    }

    void load(){
        String url = getIntent().getStringExtra("url");
        webView.loadUrl(url);
    }

    @Override
    public void finish() {
        webView.destroy();
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.pauseTimers();
    }
}